package ytdp.careagle.com.ytdp.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {
    public static String stampToDate(long time) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }

    public static String stampHMS(long time) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp
     * @return
     */

    /**
     * 判断是否为今天(效率比较高)
     *
     * @return true今天 false不是
     * @throws ParseException
     */
    public static int getDayBefore(long time) {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
//        Date date = getDateFormat().parse(day);
        cal.setTime(new Date(time));
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            return diffDay;
        }
        return 1;
    }

    public static String convertTimeToFormat(long timeStamp) {
        int dayBefore = getDayBefore(timeStamp * 1000);//几天前
        if (dayBefore == 0) {
            return "今天 " + DateUtils.stampHMS(timeStamp * 1000);
        } else if (dayBefore == -1) {

            return "昨天 " + DateUtils.stampHMS(timeStamp * 1000);
        } else if (dayBefore == -2) {

            return "前天 " + DateUtils.stampHMS(timeStamp * 1000);
        }
        return DateUtils.stampToDate(timeStamp * 1000);
//        long curTime = System.currentTimeMillis() / (long) 1000;
//        long time = curTime - timeStamp;
//
//        if (time < 60 && time >= 0) {
//            return "刚刚";
//        } else if (time >= 60 && time < 3600) {
//            return "今天 " + DateUtils.stampHMS(timeStamp);
//        } else if (time >= 3600 && time < 3600 * 24) {
//            return "今天 " + DateUtils.stampHMS(timeStamp);
//        } else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
////            return DateUtils.stampToDate(timeStamp * 1000);
//            int day = (int) (time / 3600 / 24);
//            if (day == 1) {
//                return "昨天 " + DateUtils.stampHMS(timeStamp);
//            }
//
//            if (day == 2) {
//
//                return "前天 " + DateUtils.stampHMS(timeStamp);
//            }
//            return DateUtils.stampToDate(timeStamp * 1000);
////            return time / 3600 / 24 + "天前";
//        } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
//            return DateUtils.stampToDate(timeStamp * 1000);
////            return time / 3600 / 24 / 30 + "个月前";
//        } else if (time >= 3600 * 24 * 30 * 12) {
//            return DateUtils.stampToDate(timeStamp * 1000);
////            return time / 3600 / 24 / 30 / 12 + "年前";
//        } else {
//            return "刚刚";
//        }
    }

    // 将字符串转为时间戳
    public static String getTime(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);
        } catch (ParseException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
        return re_time;
    }

    public static String stampToDate(int repaymentTime) {
        long time = (long) (repaymentTime * 1000l);
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }

    public static String stampToMonthDay(int repaymentTime) {
        long time = (long) (repaymentTime * 1000l);
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }

    public static String stampToMonth_Day(int repaymentTime) {
        long time = (long) (repaymentTime * 1000l);
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }

    public static String stampToDatePoint(int times) {

        long time = (long) (times * 1000l);
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }

    public static String stampLongToDatePoint(long time) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
        res = simpleDateFormat.format(time);
        return res;
    }
}
